﻿using Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WordCounter.Core
{
    /// <summary>
    /// 文件扫描器
    /// </summary>
    public class FileSanner
    {
        /// <summary>
        /// 扫描word文件列表
        /// </summary>
        /// <param name="dir"></param>
        /// <returns></returns>
        public static List<FileItem> ScanWordFiles(string dir)
        {
            List<FileItem> list = new List<FileItem>();
            if (!Directory.Exists(dir))
                return list;
            Queue<DirectoryInfo> queue = new Queue<DirectoryInfo>();
            DirectoryInfo dirInfo = new DirectoryInfo(dir);
            queue.Enqueue(dirInfo);

            while(queue.Count > 0)
            {
                dirInfo = queue.Dequeue();
                try
                {
                    FileInfo[] files = dirInfo.GetFiles();
                    foreach (FileInfo fi in files)
                    {
                        if (fi.Name.StartsWith("~"))
                            continue;

                        if (fi.Name.ToUpper().EndsWith(".DOC") || fi.Name.ToUpper().EndsWith(".DOCX"))
                        {
                            FileItem item = new FileItem
                            {
                                Name = fi.Name,
                                Path = fi.FullName,
                                IsDir = false,
                                Type = FileType.Word
                            };
                            list.Add(item);
                        }
                        else if (fi.Name.ToUpper().EndsWith(".PDF"))
                        {
                            FileItem item = new FileItem
                            {
                                Name = fi.Name,
                                Path = fi.FullName,
                                IsDir = false,
                                Type = FileType.PDF
                            };
                            list.Add(item);
                        }
                    }
                }
                catch { }

                //获取所有目录
                try
                {
                    DirectoryInfo[] dirs = dirInfo.GetDirectories();
                    foreach (DirectoryInfo di in dirs)
                    {
                        queue.Enqueue(di);
                    }
                }
                catch { }
            }
            
            return list;
        }
    }

    /// <summary>
    /// 文件项目
    /// </summary>
    public class FileItem : WPFBindingEntity
    {
        /// <summary>
        /// 文件名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 文件路径绝对
        /// </summary>
        public string Path { get; set; }

        /// <summary>
        /// 是否目录
        /// </summary>
        public bool IsDir { get; set; }

        public FileType Type { get; set; }

        public FileItem()
        {
            IsDir = false;
        }
    }

    public enum FileType
    {
        Word,
        PDF
    }
}
